package cn.xfdzx.android.apps.shop.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.ShopActivity;
import cn.xfdzx.android.apps.shop.bean.ShopCarListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.ShopCarListPresenter;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.view.TagTextView;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：购物车 零售
 */
public class CarRetailAdapter extends RecyclerView.Adapter<CarRetailAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<ShopCarListBeen.DataBean.ValidBean> mList;
    private Context mContext;
    private Dialog dialog;
    private ShopCarListPresenter mPresenter;


    public CarRetailAdapter(Context context, List<ShopCarListBeen.DataBean.ValidBean> mList, ShopCarListPresenter presenter) {
        this.mList = mList;
        this.mContext = context;
        this.mPresenter = presenter;
    }



    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_car_retail, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        holder.rvGoods.setLayoutManager(linearLayoutManager);
        ItemCarRetailAdapter itemCarRetailAdapter;

        if (!mList.get(position).isIs_shixiao()) {
            holder.rlTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isFastClick()) {
                        Intent intent = new Intent(mContext, ShopActivity.class);
                        intent.putExtra("shop_id", mList.get(position).getStore().getId() + "");
                        mContext.startActivity(intent);
                    }
                }
            });
            if (mList.get(position).getStore().getOffline_shop_name()!=null) {
                holder.tvShopName.setText(mList.get(position).getStore().getOffline_shop_name());
            }
            holder.ivShopSelect.setVisibility(View.VISIBLE);
            holder.tvSettlement.setVisibility(View.VISIBLE);
            holder.tvMoney.setVisibility(View.VISIBLE);
            itemCarRetailAdapter = new ItemCarRetailAdapter(mContext, mList.get(position).getGoods(), false, this, mPresenter, position );
        } else {
            holder.rlTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });
            holder.tvShopName.setText("失效商品");
            holder.ivShopSelect.setVisibility(View.INVISIBLE);
            holder.tvSettlement.setVisibility(View.INVISIBLE);
            holder.tvMoney.setVisibility(View.INVISIBLE);
            itemCarRetailAdapter = new ItemCarRetailAdapter(mContext, mList.get(position).getGoods(), true, this, mPresenter, position );
        }
        holder.tvRemoveGood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    dialog = Utils.addDialog(mContext, R.layout.dialog, R.string.are_you_sure_you_want_to_clear_out_dead_items, R.string.confirm, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

                            String ids = "";
                            for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX goodsBeanX : mList.get(position).getGoods()) {
                                if (ids.isEmpty()) {
                                    ids = goodsBeanX.getId() + "";
                                } else {
                                    ids = ids + "," + goodsBeanX.getId();
                                }
                            }
                            mPresenter.removeGood(ids + "", position, 0, true);
                            dialog.dismiss();
                        }
                    });

                dialog.show();
            }
        });

        holder.rvGoods.setNestedScrollingEnabled(false);
        holder.rvGoods.setAdapter(itemCarRetailAdapter);

        //是否已全选
        mList.get(position).setIs_select(true);
        for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX x : mList.get(position).getGoods()) {
            if (!x.isIs_select()) {
                //只要有一列  就不是全选
                mList.get(position).setIs_select(false);
            }
        }
        if (mList.get(position).isIs_select()) {
            holder.ivShopSelect.setImageResource(R.drawable.icon_select_orange);
        } else {
            holder.ivShopSelect.setImageResource(R.drawable.d99999_oval);
        }
        holder.ivShopSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mList.get(position).isIs_select()) {
                    mList.get(position).setIs_select(false);
                    holder.ivShopSelect.setImageResource(R.drawable.d99999_oval);
                    for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX x : mList.get(position).getGoods()) {
                        x.setIs_select(false);
                    }
                } else {
                    holder.ivShopSelect.setImageResource(R.drawable.icon_select_orange);
                    mList.get(position).setIs_select(true);
                    mList.get(position).setIs_all_select(true);
                    for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX x : mList.get(position).getGoods()) {
                        x.setIs_select(true);
                    }
                }
                notifyDataSetChanged();

            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });

        double price = 0;
        for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX goodsBeanX : mList.get(position).getGoods()) {
            if (goodsBeanX.isIs_select()) {
                price = Utils.addDouble(Utils.mul(goodsBeanX.getCart_num(),Double.parseDouble(goodsBeanX.getProductInfo().getPrice())), price);
            }
        }
        holder.tvMoney.setText("¥" + price);

        holder.tvSettlement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()){
                String select_id = "";
                for (int i = 0; i < mList.get(position).getGoods().size(); i++) {
                        if (mList.get(position).getGoods().get(i).isIs_select()) {
                            if (select_id.isEmpty()) {
                                select_id = mList.get(position).getGoods().get(i).getId() + "";
                            } else {
                                select_id = select_id + "," + mList.get(position).getGoods().get(i).getId();
                            }
                    }
                }
                if (select_id.isEmpty()) {
                    Toast.makeText(mContext, "请选中结算的商品", Toast.LENGTH_SHORT).show();
                }else {
                    String  user_curent_lng= UtilsData.getPreference(mContext, "coordinates").getString("coordinates_lng");
                    String  user_curent_lat= UtilsData.getPreference(mContext, "coordinates").getString("coordinates_lat");
                    mPresenter.confirm(select_id,user_curent_lng,user_curent_lat);
                }
            }}
        });
    }


    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.rv_goods)
        RecyclerView rvGoods;
        @BindView(R.id.tv_shop_name)
        TextView tvShopName;
        @BindView(R.id.iv_shop_select)
        ImageView ivShopSelect;
        @BindView(R.id.rl_title)
        RelativeLayout rlTitle;
        @BindView(R.id.tv_remove_good)
        TextView tvRemoveGood;
        @BindView(R.id.tv_settlement)
        TextView tvSettlement;
        @BindView(R.id.tv_money)
        TextView tvMoney;


        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
